﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace EBA.Data
{
    /// <summary>
    /// 两组对象的差异集。
    /// </summary>
    public class ChangeSet<TOriginalValue, TCurrentValue>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ChangeSet()
        {
            this.Modified = new Dictionary<TOriginalValue, TCurrentValue>();
            this.Deleted = new List<TOriginalValue>();
            this.Added = new List<TCurrentValue>();
        }

        /// <summary>
        /// 被修改的对象集合。
        /// </summary>
        public Dictionary<TOriginalValue, TCurrentValue> Modified
        {
            get;
            set;
        }

        /// <summary>
        /// 被删除的对象集合。
        /// </summary>
        public List<TOriginalValue> Deleted
        {
            get;
            set;
        }

        /// <summary>
        /// 新增的对象集合。
        /// </summary>
        public List<TCurrentValue> Added
        {
            get;
            set;
        }

        /// <summary>
        /// 循环处理被删除对象。
        /// </summary>
        public void ForEachDeleted(Action<TOriginalValue> action)
        {
            foreach (var i in this.Deleted)
            {
                action(i);
            }
        }

        /// <summary>
        /// 循环处理被修改对象。
        /// </summary>
        public void ForEachModified(Action<TOriginalValue, TCurrentValue> action)
        {
            foreach (var i in this.Modified)
            {
                action(i.Key, i.Value);
            }
        }

        /// <summary>
        /// 循环处理新增对象。
        /// </summary>
        public void ForEachAdded(Action<TCurrentValue> action)
        {
            foreach (var i in this.Added)
            {
                action(i);
            }
        }
    }
}
